
<template>
  <div>
    <!-- 收藏列表 -->
    <div class="collectionBox" v-show="!isAdd" v-drag="'collectionTit'">
      <div class="collectionTit" id="collectionTit">
        <p class="collectionTxt">添加到歌单</p>
        <i
          class="iconfont icon-x hideBtn"
          @click="set_change_songSheetBoxIsHide(false)"
        ></i>
      </div>
      <ul class="listBox">
        <li class="addItem" @click="chengeIsAdd">
          <span class="addIcon"></span>
          <span class="addTxt">新歌单</span>
        </li>
        <li
          v-for="item in userSongList"
          :key="item.id"
          @click="collectionMusicFn(item.id)"
        >
          <img class="ItemCoverImg" :src="item.coverImgUrl" alt="" />
          <div>
            <p class="itemName">{{ item.name }}</p>
            <p class="itemTrackCount">{{ item.trackCount }}首歌</p>
          </div>
        </li>
      </ul>
    </div>
    <!-- 添加收藏文件夹 -->
    <div
      class="AddCollectionBox"
      v-show="isAdd"
      v-drag="'dragId'"
      v-loading="addloading"
      element-loading-text="请稍后"
      element-loading-spinner="el-icon-loading"
    >
      <div class="collectionTit" id="dragId">
        <p class="collectionTxt">新建歌单</p>
        <i class="iconfont icon-x hideBtn" @click="chengeIsAdd"></i>
      </div>
      <div class="addInputBox">
        <el-form label-width="80px">
          <el-form-item label="歌单名">
            <input v-model="songSheetName" type="text" class="songSheetName" />
          </el-form-item>
        </el-form>
        <p class="notes">可通过“收藏”将音乐添加到新歌单中</p>
        <div class="btnBox">
          <div class="btnClass yesBtn" @click="addSongSheetFn">
            <i>确 定</i>
          </div>
          <div class="btnClass noBtn" @click="chengeIsAdd">
            <i>取 消</i>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Notification } from "element-ui";
import { userSongSheet, addSongSheet, collectionMusic } from "@/api";
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      userSongList: "",
      isAdd: false,
      songSheetName: "",
      addloading: false,
    };
  },
  computed: {
    ...mapState("loginStore", ["userInfo"]),
    ...mapState("songSheetStore", ["musicId"]),
  },
  created() {
    this.getSongSheet();
  },
  methods: {
    ...mapActions("songSheetStore", [
      "set_change_songSheetBoxIsHide",
      "set_change_songSheetBoxIsHide",
    ]),
    getSongSheet() {
      userSongSheet({ uid: this.userInfo.profile.userId, limit: 9999 }).then(
        (res) => {
          let arr = [];
          res.playlist.forEach((item) => {
            if (!item.subscribed) arr.push(item);
          });
          this.userSongList = arr;
          console.log(arr);
        }
      );
    },
    chengeIsAdd() {
      this.isAdd = !this.isAdd;
    },
    //添加歌單
    addSongSheetFn() {
      if (this.songSheetName.trim() == "") return;
      this.addloading = true;
      addSongSheet({ name: this.songSheetName }).then((res) => {
        this.addloading = false;
        if (res.code == 200) {
          Notification({
            message: "歌单新建成功",
            type: "success",
            duration: 1500,
          });
          this.getSongSheet();
          this.isAdd = false;
        }
      });
    },
    //收藏音樂
    collectionMusicFn(id) {
      collectionMusic({ op: "add", pid: id, tracks: this.musicId }).then(
        (res) => {
          if (res.status == 200) {
            this.set_change_songSheetBoxIsHide(false);
            if (res.body.code == 200) {
              Notification({
                message: "收藏成功",
                type: "success",
                duration: 1500,
              });
            } else {
              Notification({
                message: res.body.message,
                type: "success",
                duration: 1500,
              });
            }
          }
        }
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.collectionBox,
.AddCollectionBox {
  position: fixed;
  left: calc(50% - 265px);
  top: calc(50% - 265px);
  background: #fff;
  z-index: 99999;
  width: 480px;
  display: flex;
  flex-direction: column;
  height: 372px;
  border-radius: 4px;
  box-shadow: 0 5px 16px rgba(0, 0, 0, 0.8);
  .collectionTit {
    background: #2d2d2d;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .collectionTxt {
      padding-right: 45px;
      margin: 0;
      height: 38px;
      line-height: 38px;
      padding-left: 18px;
      border-radius: 3px 3px 0 0;
      font-weight: bold;
      font-size: 14px;
      color: #fff;
    }
    .hideBtn {
      color: #fff;
      display: block;
      padding: 10px;
    }
  }
  .listBox {
    overflow: auto;
    .addItem {
      padding: 10px 0 10px 35px;
      background: #e6e6e6;
      cursor: pointer;
      align-items: center;
      .addIcon {
        background: url("./../../assets/imges/btnIconTwo.png") no-repeat 0 -495px;
        width: 35px;
        height: 36px;
        margin-right: 10px;
        display: inline-block;
      }
      .addTxt {
        color: #333;
      }
    }
    li {
      padding: 6px 0 6px 35px;
      border-top: 1px solid #e0e0e0;
      display: flex;
      .ItemCoverImg {
        width: 40px;
        height: 40px;
        margin-right: 10px;
      }
      .itemName {
        margin-top: 2px;
        margin-bottom: 8px;
      }
      .itemTrackCount {
        color: #666;
      }
      &:hover {
        background: #f2f2f2;
      }
    }
  }
}
.AddCollectionBox {
  height: 250px;
  .addInputBox {
    padding: 40px 0;
    margin-left: 30px;
    text-align: left;
    .songSheetName {
      height: 19px;
      margin: 0;
      padding: 5px 6px 6px;
      border: 1px solid #cdcdcd;
      border-radius: 2px;
      line-height: 19px;
      box-sizing: content-box;
      vertical-align: middle;
      width: 300px;
      outline: none;
    }
    .notes {
      margin: 8px 0 20px 48px;
      color: #999;
    }
    .btnBox {
      margin-left: 48px;
      .btnClass {
        color: #333;
        background: url("./../../assets/imges/backgroundStripe.png") no-repeat;
        padding: 0 5px 0 0;
        white-space: nowrap;
        display: inline-block;
        vertical-align: middle;
        margin-right: 5px;
        i {
          padding: 0 15px 0 20px;
          height: 31px;
          line-height: 30px;
          display: inline-block;
          min-width: 23px;
          background: url("./../../assets/imges/backgroundStripe.png") no-repeat;
          cursor: pointer;
        }
      }
      .yesBtn {
        background-position: right -428px;
        color: #fff;
        i {
          background-position: 0 -387px;
        }
      }
      .noBtn {
        background-position: right -100px;
        margin-left: 20px;
        i {
          background-position: 0 -59px;
        }
      }
    }
  }
}
</style>